package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements Ioper {
    @Override
    public void work(BookList bookList) {
            System.out.println("-删减图书-");
            System.out.println("请输入你要删减图书的书名");
            Scanner sc = new Scanner(System.in);
            String name = sc.next();
            int currentSize = bookList.getUsedSize();
            int pos = -1;
            int i = 0;
            for (i = 0; i < currentSize; i++) {
                Book book = bookList.getBooks(i);
                if (book.getName().equals(name)) {
                    pos = i;
                    break;
                }
            }
            if (i > currentSize) {
                System.out.println("未找到该本书");
                return;
            }
            for (int j = pos; j < currentSize - 1; j++) {
                Book book = bookList.getBooks(j + 1);
                bookList.setBooks(j, book);
            }
            bookList.setUsedSize(currentSize - 1);
            //此时末尾的书本会重复出现因此，将其重新设置为null
            bookList.setBooks(currentSize - 1, null);
            System.out.println("删除成功！");
        }
    }

